import request from '@/config/request.js'
import { getSelfModuleSubjectApi } from '@/api/common'
import { getVideoCover } from '@/utils'
import store from '@/store'

export const getDictList = () => {
  return request.get('/app-api/system/dict/data/list-all-simple')
}

export const getLearningList = (data) => {
  return new Promise(async (resolve) => {
    const res = await request.get('/app-api/question/databaseApp/getStudyData', data)

    const {
      data: { list }
    } = res
    if (Array.isArray(list) && list.length > 0) {
      list.forEach(async (item) => {
        const { studyDataRespList } = item

        studyDataRespList.forEach((file) => {
          const { fileType, isSystemFile, fileUrl } = file

          let src =
            store.state.learning.minioConfig.fileServerUrlApp +
            '/' +
            (isSystemFile == 0 ? 'snd-ai-assistant' : store.state.learning.minioConfig.bucket) +
            fileUrl

          if (fileType == 3) {
            file['thumbnail'] =
              ''
            getVideoCover(src).then((cover) => {
              file['thumbnail'] = cover
            })
          }
        })
      })
    }

    resolve(res)
  })
}

export const getFileUrl = (data) => {
  return request.get('/admin-api/system/tenant-config/get', data)
}

export const setReadStatus = (fileId, readMethod, date) => {
  return request.post(`/app-api/question/databaseApp/startRead/${fileId}/${readMethod}/${date}`)
}

// 获取课程列表
export const getStudentCoursesList = (data) => {
  return new Promise((resolve) => {
    Promise.all([
      request.get('/question-api/question/courses/getStudentCoursesList', data),
      getSelfModuleSubjectApi()
    ]).then((res) => {
      const [subjectIdListData, moduleSubjectListData] = res
      const { data: subjectIdList } = subjectIdListData
      const { data: moduleSubjectList } = moduleSubjectListData

      // 获取已经开通的科目
      if (Array.isArray(moduleSubjectList) && moduleSubjectList.length > 0) {
        const selfSubjectList = moduleSubjectList.filter((i) => i.moduleId === '3')
        if (Array.isArray(subjectIdList) && selfSubjectList.length > 0) {
          const copySubjectIdList = JSON.parse(JSON.stringify(subjectIdList))
          const selfSubjectIds = selfSubjectList.map((i) => i.subject)
          subjectIdList.splice(0, subjectIdList.length)
          subjectIdList.push(...copySubjectIdList.filter((i) => selfSubjectIds.includes(i.value)))
        } else {
          subjectIdList.splice(0, subjectIdList.length)
        }
      } else {
        subjectIdList.splice(0, subjectIdList.length)
      }

      resolve(subjectIdListData)
    })
  })
}
